package com.scujj.controller.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.scujj.controller.SubcribeController;
import com.scujj.controller.UserController;
import com.scujj.enums.SubscribeState;
import com.scujj.pojo.Result;
import com.scujj.pojo.Subcribe;
import com.scujj.pojo.User;
import com.scujj.service.SubcribeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/subcribe")
@Api(tags = "用户关注接口")
public class SubcribeControllerImpl implements SubcribeController {
    @Autowired
    private SubcribeService subcribeService;
    @Autowired
    private UserController userController;

    @Override
    @PutMapping()
    @ApiOperation("关注其它用户")
    public Result insertSubcribe(@RequestBody HashMap<String, Long> args) {
        Long id = args.get("id");
        Long bid = args.get("beScribeId");
        if (id == null || bid == null) {
            return Result.error("参数错误");
        }
        List<User> userList = (List<User>) userController.getUserByIdList(Arrays.asList(id, bid)).getData();
        if (userList.size() != 2) {
            return Result.error("用户不存在");
        }
        QueryWrapper<Subcribe> wrapper = new QueryWrapper<>();
        wrapper.eq("uid", id);
        wrapper.eq("sid", bid);
        if (subcribeService.getOne(wrapper) != null) {
            return Result.error("重复操作");
        }
        Subcribe subcribe = new Subcribe().setUid(id).setSid(bid);
        if (subcribeService.save(subcribe)) {
            return Result.success();
        } else {
            return Result.error("网络错误");
        }
    }

    @Override
    @DeleteMapping("/delete")
    @ApiOperation("取消关注其它用户")
    public Result deleteSubcribe(@RequestBody HashMap<String, Long> args) {
        Long id = args.get("id");
        Long bid = args.get("beScribeId");
        if (id == null || bid == null) {
            return Result.error("参数错误");
        }
        QueryWrapper<Subcribe> wrapper = new QueryWrapper<>();
        wrapper.eq("uid", id).eq("sid", bid);
        if (subcribeService.remove(wrapper)) {
            return Result.success();
        } else {
            return Result.error("关注的用户不存在");
        }
    }

    @Override
    @GetMapping("/state/{uid}/{suid}")
//    @ApiOperation("查询关注状态")
    public Result subscribeState(@PathVariable String uid, @PathVariable String suid ) {
        QueryWrapper<Subcribe> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("uid",uid);
        wrapper1.eq("sid",suid);
        QueryWrapper<Subcribe> wrapper2 = new QueryWrapper<>();
        wrapper2.eq("uid",suid);
        wrapper2.eq("sid",uid);
        Subcribe s1= subcribeService.getOne(wrapper1);
        Subcribe s2= subcribeService.getOne(wrapper2);
        if((s1!=null)&&(s2!=null)){
            return Result.success(SubscribeState.EACH);
        }
        if((s1==null)&&(s2==null)){
            return  Result.success(SubscribeState.NONE);
        }

        if(s1 != null){
            return Result.success(SubscribeState.SUB);
        }

        return Result.success(SubscribeState.BE);
    }
}
